package com.zhang.boot.core.exception;

import com.zhang.boot.core.constants.ErrorEnum;
import com.zhang.boot.core.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author zhangshaobin
 * @Date 2020/10/22
 * @Description 全局异常拦截
 */
@RestControllerAdvice
@Slf4j
public class GlobalException {

    /**
     * 没有权限访问
     *
     * @param exception
     * @return
     */
    @ExceptionHandler(value = AccessDeniedException.class)
    public Result handler(AccessDeniedException exception) {
        exception.printStackTrace();
        log.error(exception.getMessage());
        return new Result(ErrorEnum.ILLEGAL_ACCESS);
    }

    /**
     * 处理业务异常
     *
     * @param exception
     * @return
     */
    @ExceptionHandler(value = BusinessException.class)
    public Result handler(BusinessException exception) {
        exception.printStackTrace();
        log.error(exception.getMessage());
        return new Result(exception.getErrorEnum());
    }
}
